using System.Collections.Generic;
using UnityEngine;

namespace Fusion.Utilities.Animations
{
    public class AnimationUtility
    {
        private static List<Vector3> points;

        static AnimationUtility()
        {
            points = new List<Vector3>();
        }

        public static Vector3 Berzier(Vector3 start, Vector3 control, Vector3 end, float t)
        {
            points.Clear();
            points.Add(start);
            points.Add(control);
            points.Add(end);
            return Berzier(points, t);
        }

        public static Vector3 Berzier(List<Vector3> points, float time)
        {
            if (points.Count == 0)
            {
                return Vector3.zero;
            }
            else if (points.Count == 1)
            {
                return points[0];
            }
            else
            {
                Mathf.Clamp01(time);
                for (int i = 0; i < points.Count; i++)
                {
                    int nextIndex = i + 1;
                    if (nextIndex < points.Count)
                    {
                        Vector3 nextPoint = points[nextIndex];
                        Vector3 newPoint = (1 - time) * points[i] + time * nextPoint;
                        points[i] = newPoint;
                    }
                }
                points.RemoveAt(points.Count - 1);

                return Berzier(points, time);
            }
        }
    }
}

